<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="附件名称" prop="oldFileName">
        <el-input
          v-model="queryParams.oldFileName"
          placeholder="请输入附件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--        style="width: 125px"-->
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          :disabled="isCreateBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="mangerList"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
      ref="table"
      border
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="left" header-align="left" />
      <el-table-column label="附件名称" min-width="400px" align="left" prop="oldFileName" >
        <template slot-scope="{ row }">
          <el-link :href="row.url" target="_blank" @click="fileDownload(row)">{{row.oldFileName}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="left" prop="reviewEvents" min-width="250" :show-overflow-tooltip="true"/>
      <el-table-column label="受评单位" align="left" prop="evaluateObject" min-width="150" :show-overflow-tooltip="true" />
      <el-table-column label="格式" align="left" prop="ext" />
      <el-table-column label="创建人" align="left" prop="createBy"/>
      <el-table-column
        label="创建时间"
        align="left"
        prop="createTime"
        min-width="150"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="left"
        width="180px"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="fileDownload(scope.row)"
            >下载</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-picture-outline"
            @click="handlePictureCardPreview(scope.row)"
            >预览</el-button
          >
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:manger:remove']"-->
<!--            >删除</el-button-->
<!--          >-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import {
  listManger,
  getManger,
  delManger,
  addManger,
  updateManger,
} from "@/api/system/manger";
import {download, listFileInfo, preview} from "@/api/system/sysFileInfo/fileInfo";
export default {
  name: "Manger",
  dicts: ["sys_normal_disable"],
  components: {
  },
  data() {
    return {
      // 按钮loading
      // buttonLoading: false,
      // 遮罩层
      loading: true,
      dialogVisible:false,
      dialogImageUrl: '',
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      isCreateBy: false,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工器具管理表格数据
      mangerList: [],
      dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ID: undefined,
        oldFileName: undefined,
        fileName: undefined,
        fileUrl: undefined,
        fileSize: undefined,
        businessKey: undefined,
        createBy: undefined,
        createTime: undefined,
        updateTime: undefined,
      },
      // 表单参数
      form: {},
      Showbox:false,
      peopleOpen:false,
      peopleOpen1:false,
      // 表单校验
      rules: {
        ID: [{ required: true, message: "主键id不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    if (this.$store.state.user.name != 'admin'){
      this.queryParams.createBy = this.$store.state.user.nikename;
      this.isCreateBy = true;
    }
    this.getList();
  },
  methods: {
    open1() {
      this.peopleOpen = true;
    },
    open2() {
      this.peopleOpen1 = true;
    },
    //选择人的确定按钮事件 submitPeople(nikeNamelist)方法传参一个默认接收用户昵称数组   submitPeople(peopleList,nikeNamelist)方法传参两个则是接收用户昵称数组和用户账号数组
    submitPeople(peopleList) {
      //console.log(peopleList);

      this.peopleOpen = false;
    },
    submitPeople1(peopleList) {
      //console.log(peopleList);
      this.peopleOpen1 = false;
    },
     handleRowClick(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    handleShow() {
      this.Showbox = !this.Showbox
      const element = this.$refs.handle_box_content;
      if(this.Showbox){
        element.style.overflow = 'visible';
        element.style.height = 'fit-content'
      }else {
        element.style.overflow = 'hidden';
        element.style.height = '50px'
      }
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listFileInfo(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
        this.mangerList = response.data;
        this.total = response.dataCout;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ID: undefined,
        oldFileName: undefined,
        fileName: undefined,
        fileUrl: undefined,
        fileSize: undefined,
        businessKey: undefined,
        createBy: undefined,
        createTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      // this.reset();
      if (this.isCreateBy == false){
        this.queryParams.createBy = '';
      }
      this.queryParams.fileName = '';
      this.queryParams.oldFileName = '';
      this.dateRange = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handlePictureCardPreview(row){
      preview(row.id).then((s) =>{
        console.log(s)
        if (s != ""){
          this.dialogImageUrl = window.URL.createObjectURL(new Blob([s], { type: "image/jpeg" }));
          this.dialogVisible = true;
          // window.open(
          //   window.URL.createObjectURL(new Blob([s], { type: "image/jpeg" }))
          // );
          return;
        }
      })
    },
    /** 按钮操作 */
    fileDownload(row) {
      this.$modal.loading("导出中")
      preview(row.id).then((s) =>{
        if (s != ""){
          // window.open(s);
          return;
        }
      })
      download(row.id).then((rew) =>{
        console.log(rew)
        this.$modal.closeLoading();
        // 创建临时的下载链接
        const url = window.URL.createObjectURL(rew);
        if (typeof window.chrome !== 'undefined') {
          // chrome
          const link = document.createElement('a');
          link.href = url;
          link.download = row.oldFileName;
          document.body.appendChild(link);
          link.click();
          setTimeout(() => {
            document.body.removeChild(link);
            URL.revokeObjectURL(url);
          }, 100);
          // window.URL.revokeObjectURL(link.href); // 释放资源
          // window.open("D:/151.pdf")
        } else if (typeof window.navigator.msSaveBlob !== 'undefined') {
          // IE
          // const blob = new Blob([rew],{ type: 'application/octet-stream' });
          window.navigator.msSaveBlob(rew,  row.oldFileName);
        } else {
          // Firefox
          const file = new File([rew],  row.oldFileName,{ type: 'application/octet-stream' });
          window.open(URL.createObjectURL(file));
        }

      }).finally(() =>{

      })

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // this.buttonLoading = true;
          if (this.form.ID != null) {
            updateManger(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                // this.buttonLoading = false;
              });
          } else {
            addManger(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                // this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const IDs = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除工器具管理编号为"' + IDs + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delManger(IDs);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/manger/export",
        {
          ...this.queryParams,
        },
        `manger_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
.handle_box_content {
  width: 81%;
  height: 50px;
  overflow: hidden;
}
.el-forms{
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
}
</style>
